/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import offset.nodes.client.chooser.model.FolderNode;
import offset.nodes.client.chooser.model.TypeIconProvider;
import offset.nodes.client.chooser.view.TreeCellTextArea;

/**
 *
 * @author Walter Lütgenau
 */
public class HeadingRenderer extends JPanel implements TreeCellRenderer {

    protected TreeCellTextArea text;

    public HeadingRenderer() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setBackground(Color.WHITE);
        
        add(text = new TreeCellTextArea());
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean isSelected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;

        setEnabled(tree.isEnabled());
        text.setText(value.toString());
        text.setSelect(isSelected);

        return this;
    }

    public Dimension getPreferredSize() {
        return text.getPreferredSize();
    }

    public void setBackground(Color color) {
        if (color instanceof ColorUIResource)
            color = null;
        super.setBackground(color);
    }

    /**
     * Return the image icon for the node type
     *
     * @param node the folder node to return the type for
     * @return the image icon
     */
    protected ImageIcon getNodeTypeIcon(FolderNode node) {
        return TypeIconProvider.getInstance().getNodeTypeIcon(node.getType());
    }
}
